import { Router } from 'express';
import { authenticate, authorize } from '../../middleware/auth';
import { UserController } from '../../admin/controllers/userController';
import { uploadAvatar } from '../../middleware/upload';
import { logOperation, skipLog } from '../../middleware/operationLog';
import { ALL_PERMISSIONS } from '../../constants/permissions';

const router = Router();
const userController = new UserController();

/**
 * @swagger
 * /api/users:
 *   get:
 *     summary: 获取用户列表
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [ACTIVE, INACTIVE, BANNED]
 *         description: 用户状态
 *       - in: query
 *         name: roleId
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     users:
 *                       type: array
 *                       items:
 *                         type: object
 *                     pagination:
 *                       type: object
 *   post:
 *     summary: 创建用户
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - email
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               email:
 *                 type: string
 *                 format: email
 *                 description: 邮箱
 *               password:
 *                 type: string
 *                 description: 密码
 *               realName:
 *                 type: string
 *                 description: 真实姓名
 *               phone:
 *                 type: string
 *                 description: 手机号
 *               roleIds:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 角色ID列表
 *     responses:
 *       201:
 *         description: 创建成功
 */
router.get('/', authenticate, authorize([ALL_PERMISSIONS.USER_LIST]), skipLog, userController.getUsers);
router.post('/', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_CREATE]),
  logOperation('用户创建', '用户管理'),
  userController.createUser
);

/**
 * @swagger
 * /api/users/stats:
 *   get:
 *     summary: 获取用户统计信息
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/stats', authenticate, authorize([ALL_PERMISSIONS.USER_READ]), skipLog, userController.getUserStats);

/**
 * @swagger
 * /api/users/{id}:
 *   get:
 *     summary: 根据ID获取用户
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 获取成功
 *   put:
 *     summary: 更新用户信息
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               email:
 *                 type: string
 *                 format: email
 *                 description: 邮箱
 *               realName:
 *                 type: string
 *                 description: 真实姓名
 *               phone:
 *                 type: string
 *                 description: 手机号
 *               roleIds:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 角色ID列表
 *     responses:
 *       200:
 *         description: 更新成功
 *   delete:
 *     summary: 删除用户
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 删除成功
 */
router.get('/:id', authenticate, authorize([ALL_PERMISSIONS.USER_READ]), skipLog, userController.getUserById);
router.put('/:id', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_UPDATE]),
  logOperation('用户更新', '用户管理'),
  userController.updateUser
);
router.delete('/:id', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_DELETE]),
  logOperation('用户删除', '用户管理'),
  userController.deleteUser
);

/**
 * @swagger
 * /api/users/{id}/reset-password:
 *   post:
 *     summary: 重置用户密码
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - newPassword
 *             properties:
 *               newPassword:
 *                 type: string
 *                 description: 新密码
 *     responses:
 *       200:
 *         description: 重置成功
 */
router.post('/:id/reset-password', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_RESET]),
  logOperation('用户密码重置', '用户管理'),
  userController.resetPassword
);

/**
 * @swagger
 * /api/users/{id}/status:
 *   patch:
 *     summary: 更新用户状态
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [ACTIVE, INACTIVE, BANNED]
 *                 description: 用户状态
 *     responses:
 *       200:
 *         description: 更新成功
 */
router.patch('/:id/status', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_UPDATE]),
  logOperation('用户状态更新', '用户管理'),
  userController.updateUserStatus
);

/**
 * @swagger
 * /api/users/{id}/enable:
 *   patch:
 *     summary: 启用用户
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 启用成功
 */
router.patch('/:id/enable', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_ENABLE]),
  logOperation('用户启用', '用户管理'),
  userController.enableUser
);

/**
 * @swagger
 * /api/users/{id}/disable:
 *   patch:
 *     summary: 禁用用户
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 禁用成功
 */
router.patch('/:id/disable', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_DISABLE]),
  logOperation('用户禁用', '用户管理'),
  userController.disableUser
);

/**
 * @swagger
 * /api/users/{id}/avatar:
 *   post:
 *     summary: 为用户上传头像
 *     tags: [用户管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               avatar:
 *                 type: string
 *                 format: binary
 *                 description: 头像文件
 *     responses:
 *       200:
 *         description: 上传成功
 */
router.post('/:id/avatar', 
  authenticate, 
  authorize([ALL_PERMISSIONS.USER_UPDATE]),
  uploadAvatar,
  logOperation('用户头像上传', '用户管理'),
  userController.uploadUserAvatar
);

export default router; 